package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.plaf.FileChooserUI;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v1;

import serializables.Song;

import domain.AudioFileFilter;
import domain.ClientDomainController;


public class ClientMainJFrame extends JFrame implements ActionListener{

	private GuiController gc;
	private JTable songTable;
	private JPanel mainPanel;
	private JButton btnPlay;
	private JButton btnPause;
	private JButton btnStop;
	private JButton btnNext;
	private JButton btnPrev;
	private JButton btnAddSong;
	private JLabel lblCurrentSong;
	
	private JFileChooser fileChooser;

	public ClientMainJFrame(GuiController gc) {
		this.gc = gc;
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				initGUI();
			}
		});
		
	}

	private void initGUI() {

		System.out.println("initiating Client GUI");
		createFrame();
		addContentPanel();
		addControls();
		openPopupConnectSettings();
		setUpSongTable();
		
		initFileChooser();
		
	}

	private void initFileChooser() {
		fileChooser = new JFileChooser(".");
		fileChooser.setFileFilter(new AudioFileFilter());
		
		
	}

	private void addControls() {
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new FlowLayout());
		controlPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		controlPanel.setSize(700,100);
		
		// Buttons
		btnPlay = new JButton("Play");
		btnPause = new JButton("Pause");
		btnStop = new JButton ("Stop");
		btnNext = new JButton("Next");
		btnPrev = new JButton("Previous");		
		controlPanel.add(btnPlay);
		controlPanel.add(btnPause);
		controlPanel.add(btnStop);
		controlPanel.add(btnNext);
		controlPanel.add(btnPrev);
		btnPlay.addActionListener(this);
		btnPause.addActionListener(this);
		btnStop.addActionListener(this);
		btnNext.addActionListener(this);
		btnPrev.addActionListener(this);
		//This class is also an ActionListener (implements it) so we don't have to
		//create a new actionListener for each Button.
		//This saves memory usage.
		//All the code should be written in the actionPerformed() method below
		
		// Currently playing
		JLabel playing = new JLabel("Currently playing:");
		controlPanel.add(playing);
		
		lblCurrentSong = new JLabel("Johnny Cash - Mean as Hell");
		controlPanel.add(lblCurrentSong);
		
		mainPanel.add(controlPanel, BorderLayout.NORTH);
	}

	private void addContentPanel() {
		mainPanel = new JPanel(new BorderLayout());
		mainPanel.setSize(new Dimension(800, 600));
		this.setContentPane(mainPanel);
		
	}

	private void createFrame() {
		this.setTitle("AudioBuddy Client 0.1");
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		
	}

	private void setUpSongTable() {
		JTable songTable = new JTable(gc.getSongTableModel());
		songTable.setVisible(true);
		JPanel tablePanel = new JPanel();
		tablePanel.setVisible(true);
		tablePanel.add(songTable);
		songTable.repaint();
		mainPanel.add(tablePanel, BorderLayout.CENTER);
		
		btnAddSong = new JButton("Add song");
		mainPanel.add(btnAddSong, BorderLayout.SOUTH);
		btnAddSong.addActionListener(this);
		
		
	}


	private void openPopupConnectSettings() {
		ConnectionSettingsFrame csf = new ConnectionSettingsFrame(this);
		
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -8935135322150787260L;

	public String getFoundServerIp() {
		// TODO Auto-generated method stub
		return gc.getFoundServerIp();
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source == btnAddSong){
			int status = fileChooser.showOpenDialog(null);
			if(status == JFileChooser.APPROVE_OPTION){
				File selectedFile = fileChooser.getSelectedFile();
				sendSongToServer(selectedFile);
				System.out.println(selectedFile.getParent());
				System.out.println(selectedFile.getName());
			}else if(status == JFileChooser.CANCEL_OPTION){
				// canceled by user
			}
		}
		
		if(source == btnPlay){
			
		}
		
		if(source == btnPause){
			
		}
		
		if(source == btnNext){
			
		}
		
		if(source == btnStop){
			
		}
		
		if(source == btnPrev){
			
		}
		
	}

	private void sendSongToServer(File selectedFile) {
		Song song = new Song();
		try {
			MP3File mp3 = new MP3File(selectedFile);
			ID3v1 tag = mp3.getID3v1Tag();
			if(tag.getArtist()!=null)
				song.setArtist(tag.getArtist());
			song.setTitle(tag.getTitle());
			song.setLength(tag.getSize());
			song.setClientIP(ClientDomainController.myOwnIp);
			ClientDomainController.sendSongToServer(song);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TagException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	

}
